package com.ywu.application.standard.wiki.service;

import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.application.standard.wiki.data.YwuWikiProjectRepository;
import com.ywu.application.standard.wiki.dto.YwuWikiProjectDto;
import com.ywu.application.standard.wiki.entity.YwuWikiProjectEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName YwuWikiProjectService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/9/20 20:38
 * @Version 1.0
 **/
@Service
public class YwuWikiProjectService {
    @Autowired
    private YwuWikiProjectRepository repository;

    /**
    * @Author GroundDemo
    * @Description 添加项目信息
    * @Date 22:05 2024/9/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> addWikiProject(YwuWikiProjectDto dto) {
        // 判断父ID是否存在
        if (StringUtils.isNotEmpty(dto.getParentProjectId())) {
            YwuWikiProjectEntity search = new YwuWikiProjectEntity();
            search.setId(dto.getParentProjectId());
            YwuWikiProjectEntity queryEntity = repository.getYwuWikiProjectEntity(search);
            if (Objects.isNull(queryEntity)) {
                return CommonResult.error().setCode("parent.id.is.not.exists").setMessage("父项目不存在");
            }
        }
        // 构建插入数据
        YwuWikiProjectEntity entity = new YwuWikiProjectEntity();
        entity.setId(TimerIdGenerateUtil.nextId());
        entity.setProjectName(dto.getProjectName());
        entity.setParentProjectId(dto.getParentProjectId());
        entity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        entity.createInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        // 插入数据
        repository.insertYwuWikiProject(entity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 更新项目信息
    * @Date 22:16 2024/9/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> updateWikiProject(YwuWikiProjectDto dto) {
        // 判断ID是否存在
        YwuWikiProjectEntity search = new YwuWikiProjectEntity();
        search.setId(dto.getId());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        YwuWikiProjectEntity queryEntity = repository.getYwuWikiProjectEntity(search);
        if (Objects.isNull(queryEntity)) {
            return CommonResult.error().setCode("id.is.not.exists").setMessage("更新目标不存在");
        }
        // 判断父ID是否存在
        if (StringUtils.isNotEmpty(dto.getParentProjectId())) {
            search.setId(dto.getParentProjectId());
            YwuWikiProjectEntity checkEntity = repository.getYwuWikiProjectEntity(search);
            if (Objects.isNull(checkEntity)) {
                return CommonResult.error().setCode("parent.id.is.not.exists").setMessage("父项目不存在");
            }
        }
        // 构建更新对象
        YwuWikiProjectEntity entity = new YwuWikiProjectEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        repository.updateYwuWikiProject(entity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除项目信息
    * @Date 22:22 2024/9/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteWikiProject(YwuWikiProjectDto dto) {
        // 删除数据
        YwuWikiProjectEntity entity = new YwuWikiProjectEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        repository.deleteYwuWikiProject(entity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 分页查询项目数据
    * @Date 22:24 2024/9/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryPageList(YwuWikiProjectDto dto) {
        dto.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        // 查询分页数量
        int count = repository.queryPageCount(dto);
        // 构建分页参数
        PageService.execPageStartAndEnd(dto.getPageInfo());
        // 查询数据
        List<YwuWikiProjectEntity> entityList = repository.queryPageList(dto);
        // 构建pageList对象
        PageList<YwuWikiProjectEntity> resPageList = new PageList<>(dto.getPageInfo().getPageIndex(), dto.getPageInfo().getPageSize(),
                count, entityList);
        return CommonResult.success(resPageList);
    }

    /**
    * @Author GroundDemo
    * @Description 查询所有项目信息
    * @Date 11:39 2024/9/21
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryAllProject() {
        String tenantId = YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId();
        List<YwuWikiProjectEntity> ywuWikiProjectEntities = repository.queryAllProject(tenantId);
        return CommonResult.success(ywuWikiProjectEntities);
    }
}
